<?php
namespace app\admin\controller;
use app\BaseController;
use think\facade\Db;
class Index extends Base{
    /*定义一个初始数组*/
    protected $info = array(
        'code'=>0,
        'msg'=>'提示',
        'total'=>0,//数据数量
        'data'=>[]
    );
    // 系统信息
    private function get_sys_info(){
        $sys_info['os']             = PHP_OS;
        $sys_info['zlib']           = function_exists('gzclose') ? 'YES' : '<font color="red">NO（请开启 php.ini 中的php-zlib扩展）</font>';
        $sys_info['safe_mode']      = (boolean) ini_get('safe_mode') ? 'YES' : 'NO';    
        $sys_info['timezone']       = function_exists("date_default_timezone_get") ? date_default_timezone_get() : "no_timezone";
        $sys_info['curl']           = function_exists('curl_init') ? 'YES' : '<font color="red">NO（请开启 php.ini 中的php-curl扩展）</font>';  
        $sys_info['web_server']     = $_SERVER['SERVER_SOFTWARE'];
        $sys_info['phpv']           = phpversion();
        $sys_info['postsize']       = @ini_get('file_uploads') ? ini_get('post_max_size') :'unknown';
        $sys_info['fileupload']     = @ini_get('file_uploads') ? ini_get('upload_max_filesize') :'unknown';
        $sys_info['max_ex_time']    = @ini_get("max_execution_time").'s'; //脚本最大执行时间
        $sys_info['set_time_limit'] = function_exists("set_time_limit") ? true : false;
        $sys_info['domain']         = $_SERVER['HTTP_HOST'];
        $sys_info['memory_limit']   = ini_get('memory_limit');
        $mysqlinfo = Db::query("SELECT VERSION() as version");
        $sys_info['mysql_version']  = $mysqlinfo[0]['version'];
        if(function_exists("gd_info")){
            $gd = gd_info();
            $sys_info['gdinfo']     = $gd['GD Version'];
        }else {
            $sys_info['gdinfo']     = "未知";
        }
        if (extension_loaded('zip')) {
            $sys_info['zip']     = "YES";
        } else {
            $sys_info['zip']     = '<font color="red">NO（请开启 php.ini 中的php-zip扩展）</font>';
        }
        return $sys_info;
    }

    public function index(){

        return $this->fetch();
    }

    public function home(){
        $logData = Db::table('an_admin_log')
        ->order('id', 'desc')
        ->page(1,20)
        ->select()
        ->toArray();
        $this->assign('logData',$logData);
        $this->assign('count_product',Db::table('an_goods')->count());
        $this->assign('count_news',Db::table('an_news')->count());
        $this->assign('sys_info',$this->get_sys_info());
        return $this->fetch();
    }

    public function search()
    {
        return $this->fetch();
    }

    public function website($add=0,$edit=0,$del=0){
        $pt = input("post.");
        if(count($pt)>0){
            if($add == 1){
                $this->logData('添加系统设置变量，'.$pt['title'].'/'.$pt['key']);
                $bool = Db::table('tb_config')->cache('config_cache')->insert($pt);
            }
            if($del == 1){
                $this->logData('删除系统设置，'.$pt['id']);
                $bool = Db::table('tb_config')->cache('config_cache')->where('id',$pt['id'])->delete();
            }
            $this->info['code']=1;
            $this->info['msg']='成功';
            return json($this->info);
        }
        $this->assign('tabType',config('an_config.config_type'));
        $this->assign('data_type',config('an_config.config_data_type'));
        return $this->fetch();
    }

    public function website_edit(){
        $pt = input("post.");
        $this->logData('修改系统设置');
        foreach($pt as $k=>$v){
            $bool = Db::table('tb_config')->cache('config_cache')->where('key', $k)->update(['value'=>$v]);
        }
        $this->info['code']=1;
        $this->info['msg']='成功';
        return json($this->info);
    }

}
